/***斗地主带机器人房间数据类
 *  
 */
var BasePkRoom = require('./../../PkCommon/BaseData/BasePkRoom');
var ClientEveDelegate = require('../../FrameWork/Delegate/ClientEveDelegate');
var MgTimeOut = require('../../FrameWork/Manager/MgTimeOut');
var ClientPkDelegateType = require('../../PkCommon/Delegate/ClientPkDelegateType');
var EnumPkRoom = require('./../../PkCommon/Enum/EnumPkRoom');
var EnumOpBtn = require('./../../PkCommon/Enum/EnumOpBtn');
var PkSound = require('./../../PkCommon/PkSound');
// var AIGetCard = require('../../PkCommon/AI/Get/AIGetCard');
var AITip = require('../../PkCommon/AI/Tip/AITip');

var _ = require('./../../FrameWork/we_underscore');

module.exports = cc.Class({
    extends: BasePkRoom,
    ctor() {

    },

    doInit() {
        this._super();
        this.callScoreInfo = [];
        this.doubleInfo = [];
        this.fristOutSeatIdx = -1; //第一个出牌的玩家
        this.landlordSeatIdx = -1; //地主
        this.bottomCardInfo = {}; /*{seatIdx: 0,cardIds: allCards }*/
    },

    /***起手发牌]
     * _roomInfo:{
     *  bankerSeatIdx:1,//庄家座位号
     *  roomState:1,//房间当前状态
     *  lastOutCardInfo:{seatIdx:1,type:1,cardIds:[]},
     *  callScoreInfo:{}//叫分
     *  doubleInfo:{},//加倍
     *  currCanOutSeatIdx:1,
     *  seatList:[
     *      {playerId:1,playName:'tony',goldCnt:1,seatIdx:1,cardIds:[],cardLeftCnt:17}
     *  ]
     * }
     */
    sendFristCard(_roomInfo) {
        this._super(_roomInfo);
        MgTimeOut.clear();
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_gamingBtnsState, EnumOpBtn.NONE);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_selfShouDouble, false);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_selfCallScoring, false);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_singleEnd, false);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllHeadUI);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllTimeOutUI);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanBottomUI);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_clearOutCardAll);
        var cards = this.resetData(_roomInfo);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_sendFristCards, cards);
    },
    /***设置完底牌之后 */
    setBottomCardsInfo(bottomCardInfo) {
        this.bottomCardInfo = bottomCardInfo;
    },
    /***重置数据 */
    resetData(_roomInfo) {
        this.refreshRoomState(_roomInfo.roomState);
        this.setBankerSeatIdx(_roomInfo.bankerSeatIdx);
        this.setLastOutCardInfo(_roomInfo.lastOutCardInfo);
        this.setCallScore(_roomInfo.callScoreInfo);
        this.setDoubleInfo(_roomInfo.doubleInfo);
        this.fristOutSeatIdx = _roomInfo.fristOutSeatIdx;
        this.setBottomCardsInfo(_roomInfo.bottomCardInfo);

        // this.setLas
        let seatList = _roomInfo.seatList;
        let size = _.size(seatList);
        let selfSeatIdx = this.getSelfSeatIdx();
        let selfCards = null;
        for (let i = 0; i < size; ++i) {
            let seatInfo = seatList[i];
            if (selfSeatIdx == seatInfo.seatIdx) {
                selfCards = seatInfo.cardIds;
            } else {
                ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_showOtherCardCnt, {
                    seatIdx: seatInfo.seatIdx,
                    cardLeftCnt: seatInfo.cardLeftCnt
                });
            }
            this.seatObjList[i].setHandCards(seatInfo.cardIds);
            this.seatObjList[i].refreshCardLeftCnt(seatInfo.cardLeftCnt);
            this.seatObjList[i].setMoney(seatInfo.money);
            ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_refreshHeadMoney, seatInfo);

        }
        /***计算可出牌座位号*/
        if (_roomInfo.roomState >= EnumPkRoom.WAITFRITING && _roomInfo.roomState < EnumPkRoom.GAMINGEND) {
            this.setCurrOutSeatIdx(_roomInfo.currCanOutSeatIdx);
        }
        let isCallScoring = (EnumPkRoom.CALLSCORING == this.getRoomState() && this.getBankerSeatIdx() == this.getSelfSeatIdx())
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_selfCallScoring, isCallScoring);
        return selfCards;
    },
    /***设置叫分数据
     * _callScoreInfo:[0,0,0]
     */
    setCallScore(_callScoreInfo) {
        this.callScoreInfo = _callScoreInfo;
    },
    /***设置加倍数据
     * _doubleInfo:[0,0,0]
     */
    setDoubleInfo(_doubleInfo) {
        this.doubleInfo = _doubleInfo;
    },

    /**设置当前可出牌的玩家_seatIdx*/
    setCurrOutSeatIdx(_seatIdx) {
        this._super(_seatIdx);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllTimeOutUI);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllShowUI);
        if (this.isSelfOutCard()) {
            var state = EnumOpBtn.NONE; //this.isResetOutCard() ? EnumOpBtn.MUSTCHOICECARD : EnumOpBtn.GET_OTHER_CARD;
            if (this.isResetOutCard()) {
                state = EnumOpBtn.MUSTCHOICECARD;
            } else {
                var selfResult = this.getSelfTipOutCard();
                //在这里处理是否要得起
                if (selfResult.isFind) {
                    state = EnumOpBtn.GET_OTHER_CARD;
                } else {
                    state = EnumOpBtn.YBQ;
                }
            }
            ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_gamingBtnsState, state);
        } else {
            ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_gamingBtnsState, EnumOpBtn.NONE);
        }
    },

    /***出牌动作
     * _outCardInfo:{seatIdx:1,type:1,cardIds:[]}
     */
    outCardAct(_outCardInfo) {
        PkSound.outCard(_outCardInfo);
        var cardLeftCnt = this._super(_outCardInfo);
        if (this.selfSeatIdx != _outCardInfo.seatIdx) {
            ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_otherOut, _outCardInfo);
        }
        return cardLeftCnt;
    },

    /**自己操作不出牌 */
    selfNoOut() {
        this.noOut({
            seatIdx: this.getSelfSeatIdx()
        });
    },

    /***要不起 
     * info:{
     *   seatIdx:1
     * }
     */
    noOut(info) {
        PkSound.randNoOut();
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllTimeOutUI);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllShowUI);
        var nextOutCardSeatIdx = this._super(info);
        if (null != this.lastOutCardInfo) {
            if (this.lastOutCardInfo.seatIdx == nextOutCardSeatIdx) {
                //没人要的起可以重新出任意牌型 
                ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_clearOutCardAll);
            }
        }
        /**逻辑处理部分 */
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_show_no_out_ui, info);
        return nextOutCardSeatIdx;
    },

    /***刷新叫分
     * _callScoreInfo:{
     * seatIdx:1,
     * score:1
     * }
     * return -1//返回下一个操作的玩家
     */
    refreshCallScore(_callScoreInfo) {
        var seatIdx = _callScoreInfo.seatIdx;
        var score = _callScoreInfo.score;
        _callScoreInfo.sex = this.getSex(seatIdx);
        this.callScoreInfo[seatIdx] = score;
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_callScorResult, _callScoreInfo);
        var isNeedNext = true;

        if (3 == score) {
            isNeedNext = false;
        } else {
            var _finds = _.filter(this.callScoreInfo, function(_score) {
                return (-1 != _score);
            });
            isNeedNext = !(_.size(_finds) == this.getMaxSeatCnt())
        }
        /***全部选过了 ,或者直接选了3分*/
        if (false == isNeedNext) {
            var _baseScore = -1;
            _.each(this.callScoreInfo, function(_callScore, _idx) {
                if (_baseScore <= _callScore) {
                    this.landlordSeatIdx = Number(_idx);
                }
            }.bind(this));
            setTimeout(function() {
                this.refreshRoomState(++this.roomState);
            }.bind(this), _.random(200, 1000));
            ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllTimeOutUI);
            ClientEveDelegate.doDataToLister(ClientPkDelegateType.Server_Sure_Landlord, this.landlordSeatIdx);
            return -1;
        }

        /***下一个叫分得玩家 */
        var nextCallScoreSeatIdx = this.getNextSeatIdx(seatIdx);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllTimeOutUI);
        return nextCallScoreSeatIdx;
    },

    /***刷新加倍
     * _doubleInfo:{
     * seatIdx:1,
     * double:1
     * }
     */
    refreshDoubleInfo(_doubleInfo) {
        var seatIdx = _doubleInfo.seatIdx;
        var double = _doubleInfo.double;
        _doubleInfo.sex = this.getSex(seatIdx);
        this.doubleInfo[seatIdx] = double;
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_doubleResult, _doubleInfo);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_showHeadDoubleUI, _doubleInfo);

        var _finds = _.filter(this.doubleInfo, function(_double) {
            return (-1 != _double);
        });

        /***全部选过了 */
        if (_.size(_finds) == this.getMaxSeatCnt()) {
            setTimeout(function() {
                ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllTimeOutUI);
                this.refreshRoomState(++this.roomState);
            }.bind(this), _.random(500, 500));
            return -1;
        }
    },

    getNextSeatIdx(_seatIdx) {
        var maxSeatCnt = this.getMaxSeatCnt() - 1;
        if (_seatIdx < maxSeatCnt) {
            return ++_seatIdx;
        } else {
            return 0;
        }
    },

    /***刷新房间状态 */
    refreshRoomState(_roomState) {
        this._super(_roomState);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_cleanAllShowUI);
        if (EnumPkRoom.SENDBOTTOMCARDS == _roomState) {
            this.refreshRoomState(EnumPkRoom.DOUBLING);
            this.bottomCardInfo.seatIdx = this.landlordSeatIdx;
            this.addHandCards(this.bottomCardInfo);
            ClientEveDelegate.doDataToLister(ClientPkDelegateType.Server_addBottomCardsToHand, this.bottomCardInfo);

        } else if (EnumPkRoom.DOUBLING == _roomState) {
            setTimeout(function() {
                ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_selfShouDouble, true);
            }.bind(this), 3000);
        } else if (EnumPkRoom.WAITFRITING == _roomState) {
            if (this.fristOutSeatIdx == this.selfSeatIdx) {
                this.setCurrOutSeatIdx(this.fristOutSeatIdx);
            }
        }
    },

    /***自己是否有加倍 */
    getSelfDoubleValue() {
        return this.doubleInfo[this.selfSeatIdx];
    },

    /**地主座位*/
    getLandlordSeatIdx() {
        return this.landlordSeatIdx;
    },
    getIsLandlord(_seatIdx) {
        return _seatIdx == this.landlordSeatIdx;
    },
    /**_seatIdx是否为地主*/
    isLandlordSeatIdx(_seatIdx) {
        return _seatIdx == this.getLandlordSeatIdx();
    },
    /**自己是否为地主*/
    isSelfLandlord() {
        return this.isLandlordSeatIdx(this.getSelfSeatIdx());
    },
    /**自己是否必须出牌 */
    isSeleMustOutCard() {
        /**抓牌之后出第一张牌如果自己是庄家就必须自己出牌 */
        if (null == this.lastOutCardInfo) {
            return this.isSelfLandlord();
        } else {
            /**轮到自己出牌时最后一次是自己出的牌是自己必须重新出牌*/
            return this.lastOutCardInfo.seatIdx == this.selfSeatIdx;
        }
    },
    timeOutOutCard() {
        if (this.isSeleMustOutCard()) {
            // timeOutCardIds
            var cardIds = this.seatObjList[0].timeOutCardIds();
            ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_timeOutCardSelfCtr, cardIds);
        } else {
            this.noOut({
                seatIdx: 0
            });
        }
    },
    getSelfTipOutCard() {
        var handCards = this.seatObjList[this.selfSeatIdx].getHandCards();
        var result = AITip.doAI(handCards, this.lastOutCardInfo);
        return result;
    },

    /**换桌 */
    doChangDesk() {
        this._super();
        this.serverFirstSendCard();
    },
    /**继续 */
    doContinue() {
        this._super();
        this.serverFirstSendCard();
    },

    /** 
     * @param {结算数据} _endInfo={ 
     * }
     */
    doGameEnd(_endInfo) {
        this._super(_endInfo);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_singleEnd, true);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_singleEndShowResultUI, _endInfo);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_hideLeftCardCntUI);
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_singleEndShowSelfHandCard);
        this.each(function(seatObj) {
            var seatInfo = seatObj.getSeatInfo();
            if (seatInfo.seatIdx != this.selfSeatIdx) {
                ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_showHandsCardBySeatIdx, seatInfo);
            }
        }.bind(this));
    },

    /**往手牌增加牌 */
    addHandCards(_bottomCardInfo) {
        var seatIdx = _bottomCardInfo.seatIdx;
        var cardIds = _bottomCardInfo.cardIds;
        this.seatObjList[seatIdx].addHandCards(cardIds);
    },
});